package com.hluck.goodweatherapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hluck.goodweatherapp.databinding.ItemRecommendCityBinding;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @Author： LJH
 * @Time： 2024/1/8
 * @description：
 */
public class RecommendCityAdapter extends RecyclerView.Adapter<RecommendCityAdapter.ViewHolder> {

    private final List<String> cities;

    private OnClickItemCallback clickItemCallback;

    public RecommendCityAdapter(List<String> cities) {
        this.cities = cities;
    }

    public void setClickItemCallback(OnClickItemCallback clickItemCallback) {
        this.clickItemCallback = clickItemCallback;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        ItemRecommendCityBinding binding = ItemRecommendCityBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        ViewHolder viewHolder = new ViewHolder(binding);
        binding.getRoot().setOnClickListener(v -> {
            if (clickItemCallback != null){
                clickItemCallback.onItemClick(viewHolder.getAdapterPosition());
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull RecommendCityAdapter.ViewHolder holder, int position) {
        holder.mBinding.tvRecommendCityName.setText(cities.get(position));
    }

    @Override
    public int getItemCount() {
        return cities.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public ItemRecommendCityBinding mBinding;

        public ViewHolder(@NonNull @NotNull ItemRecommendCityBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }
    }
}
